<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>开票</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">
        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>项目信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">项目信息</label>
                        <div class="layui-input-block">
                            <input type="text" id="enginid_name" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>收入合同信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">收入合同名称</label>
                        <div class="layui-input-block">
                            <input type="text" id="shiid_name" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">已开票金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="kaifee" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>本次申请开票</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">开票单号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">发票类型</label>
                        <div class="layui-input-block">
                            <input type="text" id="piaotype_name" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">开票主题</label>
                        <div class="layui-input-block">
                            <input type="text" id="title" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">本次开票金额含税(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="money" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled moneyinput">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">税率(%)</label>
                        <div class="layui-input-block">
                            <input type="text" id="rate" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled moneyinput">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">税额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="ratefee" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled moneyinput">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">受票单位</label>
                        <div class="layui-input-block">
                            <input type="text" id="shouer" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">统一社会信用代码</label>
                        <div class="layui-input-block">
                            <input type="text" id="ratenum" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">开户行</label>
                        <div class="layui-input-block">
                            <input type="text" id="unitbkname" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">账号</label>
                        <div class="layui-input-block">
                            <input type="text" id="unitbknum" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">地址</label>
                        <div class="layui-input-block">
                              <input type="text" id="unitaddr" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">电话</label>
                        <div class="layui-input-block">
                              <input type="text" id="unittel" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>



            <div class="buisness2 layui-hide">
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="edittits"><span>本次开票收取税费</span></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md6">
                            <label class="layui-form-label">本次开票应计税费</label>
                            <div class="layui-input-block">
                                <input type="text" id="yingshuifee" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <label class="layui-form-label">税费收取说明</label>
                            <div class="layui-input-block">
                                <input type="text" id="shuishuo" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md6">
                            <label class="layui-form-label">税费收取方式</label>
                            <div class="layui-input-block">
                                <input type="text" id="shuiqutype_name" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                            </div>
                        </div>
                        <div class="layui-col-md6 shuiqutype3 layui-hide">
                            <label class="layui-form-label">关联费用扣收</label>
                            <div class="layui-input-block">
                                <input type="text" id="kouid_name" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                            </div>
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>开票信息</span></div>
                </div>
            </div>
            <div>
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md6">
                            <label class="layui-form-label"><text>*</text>开票日期</label>
                            <div class="layui-input-block">
                                <input type="text" name="dt" id="dt" placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <label class="layui-form-label">经手人</label>
                            <div class="layui-input-block">
                                <input type="text" name="baner" id="baner" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item height_auto">
                    <div class="short_table_list" key="1">
                        <div class="opt-btn-group">
                            <div><span>发票明细</span></div>
                            <div>
                                <input type="file" id="uploadtrip" value="" accept="image/*,application/pdf" style="display: none"/>
                                <button type="button" onclick="trip_piao_data()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">发票识别</button>
                                <button type="button" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">粘贴新增</button>
                                <button type="button" onclick="import_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">Excel导入</button>
                                <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                                <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                                <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                                <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                            </div>
                        </div>
                        <table class="layui-table" lay-size="sm">
                            <thead>
                            <tr>
                                <th width="45">序号</th>
                                <th>发票号码</th>
                                <th>不含税金额(元)</th>
                                <th>税率(%)</th>
                                <th>税额(元)</th>
                                <th>价税合计(元)</th>
                                <th>备注</th>
                            </tr>
                            </thead>
                            <tbody class="short_table_tbody" id="list_list"></tbody>
                        </table>
                        <div class="listerror">提示：<span></span></div>
                    </div>
                </div>
            </div>

            <?php include '../public/upinfo.php'?>
        </div>




    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;

    $("#commentForm").validate({
        rules: {
            dt: {required: true},
        },
        messages:{
            dt: {required: "请选择"},
        }
    });

    function import_list() {
        openPage('../public/import.php?reveal=piao_kai@importdata', '770px', '490px');
    }

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_piao_kai_kai, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                var engininfo = res.data.engininfo;
                var shiinfo = res.data.shiinfo;
                var kouidinfo = res.data.kouidinfo;

                if(engininfo){
                    $("#enginid_name").val(engininfo.name);
                    if(engininfo.business == 2){
                        $(".buisness2").removeClass('layui-hide');
                    }
                    $("#kaifee").val(engininfo.kai_money);
                }
                if(shiinfo){
                    $("#shiid_name").val(shiinfo.name);
                }




                $("#number").val(data.number);
                $("#piaotype_name").val(data.piaotype_name);
                $("#title").val(data.title);
                $("#money").val(data.money);
                $("#rate").val(data.rate);
                $("#ratefee").val(data.ratefee);
                $("#shouer").val(data.shouer);
                $("#ratenum").val(data.ratenum);
                $("#unitbkname").val(data.unitbkname);
                $("#unitbknum").val(data.unitbknum);
                $("#unitaddr").val(data.unitaddr);
                $("#unittel").val(data.unittel);
                $("#shiaddr").val(data.shiaddr);
                $("#kaier_name").val(data.kaier_name);
                $("#dt").val(data.dt);
                $("#baner").val(data.baner);
                $("#duier").val(data.duier);
                $("#piaotype").val(data.piaotype);
                $("#explain").val(data.explain);

                $("#yingshuifee").val(data.yingshuifee);
                $("#shuishuo").val(data.shuishuo);
                $("#shuiqutype_name").val(data.shuiqutype_name);

                if(data.shuiqutype == 3) {
                    $(".shuiqutype3").removeClass('layui-hide');
                    if(kouidinfo){
                        $("#kouid_name").val(kouidinfo.number+'（'+kouidinfo.money+'）');
                    }
                }




                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" class="layui-input nums"></td>';
                listAddStr += '<td><input type="number" class="layui-input fee"></td>';
                listAddStr += '<td><input type="number" class="layui-input rate"></td>';
                listAddStr += '<td><input type="number" class="layui-input ratefee"></td>';
                listAddStr += '<td><input type="number" class="layui-input money"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr,1);

                var list = res.data.list;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="text" class="layui-input nums" value="'+list[key].nums+'"></td>';
                        str += '<td><input type="number" class="layui-input fee" value="'+list[key].fee+'"></td>';
                        str += '<td><input type="number" class="layui-input rate" value="'+list[key].rate+'"></td>';
                        str += '<td><input type="number" class="layui-input ratefee" value="'+list[key].ratefee+'"></td>';
                        str += '<td><input type="number" class="layui-input money" value="'+list[key].money+'"></td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';
                    }
                    $("#list_list").html(str);
                }else{
                    $("#list_list").html(listAddStr);
                }


                initParseMoney();
                public_upload_file_load('upload_files', res.data.files);
                render_from();
            }
        });


        form.on('submit(add)', function(data){



            var validRes = $("#commentForm").valid();
            if(!validRes){return false;}


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var nums = $(this).find('.nums').val();
                    var fee = $(this).find('.fee').val();
                    var rate = $(this).find('.rate').val();
                    var ratefee = $(this).find('.ratefee').val();
                    var money = $(this).find('.money').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!nums && !fee && !rate && !ratefee && !money && !explain){
                        return false;
                    }

                    list.push({
                        nums:nums,
                        fee:fee,
                        rate:rate,
                        ratefee:ratefee,
                        money:money,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');

            facade.ajax({url: ask_piao_kai_kai, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });






    var ctrlv_field_name = [
        {field: 'nums', name:'发票号码'},
        {field: 'fee', name:'不含税金额(元)',type:'number'},
        {field: 'rate', name:'税率(%)',type:'number'},
        {field: 'ratefee', name:'税额(元)',type:'number'},
        {field: 'money', name:'价税合计(元)',type:'number'},
        {field: 'explain', name:'备注'},
    ];


    function ctrlv_data_show(list) {
        if(list.length > 0){
            var nums = $("#list_list tr").length;
            for (var key in list){
                var str = '';
                nums++;
                str += '<tr>';
                str += '<td style="width: 45px">'+nums+'</td>';
                str += '<td><input type="text" class="layui-input nums" value="'+list[key].nums+'"></td>';
                str += '<td><input type="number" class="layui-input fee" value="'+list[key].fee+'"></td>';
                str += '<td><input type="number" class="layui-input rate" value="'+list[key].rate+'"></td>';
                str += '<td><input type="number" class="layui-input ratefee" value="'+list[key].ratefee+'"></td>';
                str += '<td><input type="number" class="layui-input money" value="'+list[key].money+'"></td>';
                str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                str += '</tr>';
                $("#list_list").append(str);
            }
        }
        render_date();
        render_from();
    }



    function trip_piao_data() {
        $("#uploadtrip").val();
        $("#uploadtrip").click();
    }

    $("#uploadtrip").change(function () {
        var loading = layui.layer.load(0, {shade: 0.1});
        let fd = new FormData();
        fd.append('file', this.files[0]);
        fd.append('token', facade.getCookie('token'));
        $.ajax({
            url: server_api_name+ask_upload_piaoshou_file,
            type: "POST",
            data: fd,
            contentType: false,
            processData: false,
            dataType: "json",
            success: function (res) {
                $("#uploadtrip").val();
                layui.layer.close(loading);
                if (res.code === 2000) {
                    var fileinfo = res.data.fileinfo;
                    var piaoinfo = res.data.piaoinfo;


                    //处理发票明细信息
                    var str = '';
                    var hang_num = $("#list_list tr").length;
                    str += '<tr>';
                    str += '<td style="width: 45px">'+(parseInt(hang_num)+1)+'</td>';
                    str += '<td><input type="text" class="layui-input nums" value="'+piaoinfo.nums+'"></td>';
                    str += '<td><input type="number" class="layui-input fee" value="'+piaoinfo.fee+'"></td>';
                    str += '<td><input type="number" class="layui-input rate" value="'+piaoinfo.rate+'"></td>';
                    str += '<td><input type="number" class="layui-input ratefee" value="'+piaoinfo.ratefee+'"></td>';
                    str += '<td><input type="number" class="layui-input money" value="'+piaoinfo.money+'"></td>';
                    str += '<td><input type="text" class="layui-input explain" value="'+piaoinfo.explain+'"></td>';
                    str += '</tr>';
                    $("#list_list").append(str);


                    //处理附件信息
                    var filename = fileinfo.name;
                    var filepath = fileinfo.encpath;
                    var fileurl = get_file_complete(filepath);
                    var id = fileinfo.id;
                    var filesize = fileinfo.size;
                    var fileext = fileinfo.ext;
                    var created_user = fileinfo.created_user;
                    var created_at = fileinfo.created_at;

                    var imgstr = '';
                    imgstr += '<div class="filebb" id='+id+'>';
                    imgstr += '<div class="tu">';
                    if(fileext == 'png' || fileext == 'pneg' || fileext == 'jpg' || fileext == 'jpeg' || fileext == 'gif'){
                        imgstr += '<img src="'+fileurl+'">';
                    }else{
                        imgstr += '<div class="zimu">'+fileext+'</div>';
                    }
                    imgstr += '</div>';
                    imgstr += '<div class="titss">'+filename+'</div>';
                    if(fileext == 'png' || fileext == 'pneg' || fileext == 'jpg' || fileext == 'jpeg' || fileext == 'gif'){
                        imgstr += '<i class="layui-icon layui-icon-search cha" onclick="upload_files_viewer_imgnew_show(\''+filepath+'\',\''+fileurl+'\')" title="查看"></i>';
                    }else if(fileext == 'pdf'){
                        imgstr += '<i class="layui-icon layui-icon-search cha" onclick="upload_files_viewer_pdf_show(\''+fileurl+'\')" title="查看"></i>';
                    }else if(fileext == 'doc' || fileext == 'docx' || fileext == 'xls' || fileext == 'xlsx' || fileext == 'ppt' || fileext == 'pptx'){
                        imgstr += '<i class="layui-icon layui-icon-search cha" onclick="upload_files_vieweroffice_show(\''+fileurl+'\')" title="查看"></i>';
                    }else{
                        imgstr += '<i class="layui-icon layui-icon-search cha" onclick="upload_files_show_yulan_not()" title="查看"></i>';
                    }

                    imgstr += '<i class="layui-icon layui-icon-delete del" onclick="upload_files_del_filenew(this)" title="删除"></i>';
                    imgstr += '</div>';


                    $('#fileboxss').append(imgstr);


                    render_from();
                }else{
                    facade.error(res.msg);
                }
            },
            error: function (res) {
                $("#uploadtrip").val();
                layui.layer.close(loading);
                facade.error("网络错误");
            },
        })
    });
</script>
</body>
</html>
